<?php

/**
 * @version		1.5.x
 * @package		JoomlaVi Project
 * @copyright	Copyright (C) JoomlaVi. All rights reserved.
 * @license		Proprietary - JoomlaVi Club members only
 * @email       info@joomlavi.com 
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

/**
 * Editor Image buton
 *
 * @package Editors-xtd
 * @since 1.5
 */
class plgButtonJVGallery extends JPlugin
{
	/**
	 * Constructor
	 *
	 * For php4 compatability we must not use the __constructor as a constructor for plugins
	 * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
	 * This causes problems with cross-referencing necessary for the observer design pattern.
	 *
	 * @param 	object $subject The object to observe
	 * @param 	array  $config  An array that holds the plugin configuration
	 * @since 1.5
	 */
	function plgButtonJVGallery(& $subject, $config)
	{
		parent::__construct($subject, $config);
	}

	/**
	 * Display the button
	 *
	 * @return array A two element array of ( imageName, textToInsert )
	 */
	function onDisplay($name)
	{
		$mainframe  = JFactory::getApplication();
        $lang       = JFactory::getLanguage();
        $lang->load('plg_system_jvgallery');
        $parameter  = new JParameter('', JPATH_ROOT.'/plugins/editors-xtd/jvgallery/params.xml');
		$doc 		=& JFactory::getDocument();
		$template 	= $mainframe->getTemplate();
        
		// button is not active in specific content components
		$getContent = $this->_subject->getContent($name);
		$present = JText::_('ALREADY EXISTS', true) ;
        $script  = array();
        $replace = array("\r\n", "\n", "\t", "params[", "]", "params");
        
        $script[]="function insertJVGallery(editor){ var content = $getContent;";   
        $script[]= '  var jvgalleryData  = \'<form action="" name="jvgalleryForm" id="jvgalleryForm" \';';
        $script[]= '    jvgalleryData += "onsubmit=\"return insertGallery(\'"+editor+"\'); SqueezeBox.listeners.close(); \">";';  
        $script[]= '    jvgalleryData += \'<table width="100%"><tr><td class="key" colspan="2" align="right"><input type="submit" name="pfSubmit" id="pfSubmit" value="Insert" /> <input type="reset" name="pfNo" id="pfNo" value="Reset"/></td></tr></table>\';';
        $script[]= '    jvgalleryData += \'<form>\';';
        $script[]= '    jvgalleryData += \'<fieldset><legend>Content Tabs</legend>\';';
        $script[]= "    jvgalleryData += '".str_replace($replace,'',$parameter->render())."'";
        $script[]= '    jvgalleryData += \'</fieldset>\'';
        $script[]= '    jvgalleryData += \'</form>\';';        
        $script[]="   var jvgallery = new Element('div', {";
        $script[]="                 'class': 'jvtab' ";
        $script[]="                });";
        $script[]="    jvgallery.setHTML(jvgalleryData); ";
        $script[]="    SqueezeBox.fromElement(jvgallery, {";
        $script[]="         handler: 'adopt',";
        $script[]="         size: {x: 500, y: 430},";
        $script[]="         onOpen: function(){
                                var JTooltips = new Tips($$('.hasTip'), { maxTitleChars: 50, fixed: false});
                                $('folder').addEvent('change', function() {
                                    changeValue('caption',list, this.value);
                                });
                }";
        $script[]="    });";
        $script[]="    return false;";  
        $script[]="};";

        
        $doc->addScriptDeclaration(implode("\n", $script));
        
		$doc->addScript(JURI::root().'plugins/editors-xtd/jvgallery/js/jvgallery-xtd.js');
        $doc->addStyleDeclaration('.hidden {display:none} .tool-tip {z-index: 99999 !important}');
		$button = new JObject();
		$button->set('modal', false);
		$button->set('onclick', 'insertJVGallery(\''.$name.'\');return false;');
		$button->set('text', JText::_('Insert Gallery ...'));
		$button->set('name', 'end');
		// TODO: The button writer needs to take into account the javascript directive
		$button->set('link', '#');
        
        return $button;
	}
}
